package ie.live.byrnep.pleasesleephere.base;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Json {

	private JSONObject json;
	private Object obj;
	private HashMap<String, Object> hm;
	private Map<String, Object> roomMap = new HashMap<String, Object>();
	
	@SuppressWarnings("unchecked")
	public JSONObject encodeString(String keyValue, String searchValue) {
		json = new JSONObject();
		json.put(keyValue, searchValue);
		return json;
	}

	public JSONArray getUrlArray(String urlKeyString) {
		obj = JSONValue.parse(urlKeyString);
		JSONArray array = (JSONArray) obj;
		return array;
	}


	public JSONObject getJsonObj(final String roomDetails) {
		json = new JSONObject();
		try {
			json = (JSONObject) new JSONParser().parse(roomDetails);
		} catch (ParseException e) {}
		return json;
	}

	//TODO redesign hash map
	
	/*
	 * HashMap<String, Object> map = new HashMap<String, Object>();	
		JSONObject e = monrestodb1.getJSONObject(i);
	 */
	
	public HashMap<String, Object> getHashMap(final String roomDetails, final String urlKeyString, 
			final String stringUrl, final String ImageUrl) {
		json = new JSONObject();
		json = getJsonObj(roomDetails);
		hm = new HashMap<String, Object>();
		hm.put("title", json.get("title").toString());
		hm.put("des", json.get("description").toString());
		hm.put("price", json.get("price").toString());
		hm.put("key", urlKeyString);
		
		
		//hm.put("pic_url", json.get("pic_url").toString());
		URL aURL;
		try {
			String tempUrl = json.get("pic_url").toString();
			tempUrl = tempUrl.substring(0,tempUrl.length()-1) + "0";
			aURL = new URL(json.get("pic_url").toString());
			Bitmap bitmap = BitmapFactory.decodeStream(aURL.openStream());
			int i=3;
			hm.put("photo", bitmap);
			int e=4;
			i+=e;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
		return hm;
	}

	public Map<String, Object> getRoomMap(final String roomDetails, final String imageUrl) {
		json = new JSONObject();
		json = getJsonObj(roomDetails);
		roomMap.put("title", json.get("title").toString());
		roomMap.put("description", json.get("description").toString());
		/* * description */
		roomMap.put("price", json.get("price").toString());
		roomMap.put("img_url", json.get("pic_url").toString());
		roomMap.put("house_type", json.get("house_type").toString());
		roomMap.put("room_type", json.get("room_type").toString());
		roomMap.put("bathroom_count", json.get("bathroom_count").toString());
		roomMap.put("smoking", json.get("smoking").toString());	
		roomMap.put("gender", json.get("gender").toString());
		roomMap.put("age", json.get("age").toString());
		/* * amentities */
		roomMap.put("tv", json.get("tv").toString());
		roomMap.put("sky_cable", json.get("sky_cable").toString());
		roomMap.put("onsuite", json.get("onsuite").toString());
		roomMap.put("internet", json.get("internet").toString());
		roomMap.put("heating", json.get("heating").toString());
		roomMap.put("air_con", json.get("air_con").toString());
		roomMap.put("user", json.get("user_id").toString());

		
		return roomMap;
	}

	public JSONObject encodeLocCord(final String latitude,final String longitude,final int distanceChoice) {
		json = new JSONObject();
		json.put("latitude", latitude);
		json.put("longitude", longitude);
		json.put("max_distance", distanceChoice);
		return json;
	}

	public JSONObject encodeRoomRequestMessage(MessageDetails messageDetails) {
		json = new JSONObject();
		json.put("message_to", messageDetails.getTo());
		json.put("message_from", messageDetails.getFrom());
		json.put("message_subject", messageDetails.getSubject());
		json.put("message_content", messageDetails.getContent());
		
		return json;
	}

}
